var arr = [1, [2, [3, 4]]];

function flatten(arr, n) {
  var result = [];
  // n--;
  for (var i = 0, len = arr.length; i < len; i++) {
    // if (Array.isArray(arr[i]) && n > 0) {
    if (Array.isArray(arr[i])) {
      result = result.concat(flatten(arr[i], n))
    } else {
      result.push(arr[i])
    }
  }
  return result;
}

function flatten(arr) {
  var result = [];
  arr.forEach(item => {
    if (Array.isArray(item)) {
      result = result.concat(flatten(item))
    } else {
      result.push(arr[i])
    }
  })
  return result;
}

function flatten(arr) {
  return arr.reduce((pre, cur) => {
    return pre.concat(Array.isArray(cur) ? flatten(cur) : cur)
  }, [])
}

function flatten(arr) {
  return arr.toString().split(',').map(function (item) {
    return +item
  })
}